package com.xdog.okx.controller;

import com.alibaba.fastjson.JSONObject;
import com.xdog.common.core.domain.R;
import com.xdog.okx.domain.*;
import com.xdog.okx.domain.InternalTransactionMultiQuery;
import com.xdog.okx.domain.LargeTransactionListQuery;
import com.xdog.okx.domain.TokenTransactionQuery;
import com.xdog.okx.domain.TokenTransferMultiQuery;
import com.xdog.okx.domain.TransactionFillsQuery;
import com.xdog.okx.domain.TransactionListQuery;
import com.xdog.okx.domain.TransactionMultiQuery;
import com.xdog.okx.domain.UnconfirmedTransactionListQuery;
import com.xdog.okx.service.IOkxTransactionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * OKX 交易数据控制器
 */
@Tag(name = "OKX 交易数据", description = "OKX 交易数据控制器")
@RestController
@RequestMapping("/okx/transaction")
public class OkxTransactionController {

    @Resource
    private IOkxTransactionService okxTransactionService;

    /**
     * 查询公链交易列表
     */
    @Operation(summary = "查询公链交易列表", description = "根据链短名称查询公链交易列表")
    @GetMapping("/list")
    public R<JSONObject> getTransactionList(TransactionListQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            if (query.getBlockHash() != null) params.put("blockHash", query.getBlockHash());
            if (query.getHeight() != null) params.put("height", query.getHeight());
            if (query.getLimit() != null) params.put("limit", query.getLimit());
            if (query.getPage() != null) params.put("page", query.getPage());

            JSONObject transactionList = okxTransactionService.getTransactionList(params);
            return R.ok(transactionList);
        } catch (Exception e) {
            return R.fail("查询公链交易列表失败: " + e.getMessage());
        }
    }

    /**
     * 查询公链大额交易列表
     */
    @Operation(summary = "查询公链大额交易列表", description = "根据链短名称查询公链大额交易列表")
    @GetMapping("/large-list")
    public R<JSONObject> getLargeTransactionList(LargeTransactionListQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            if (query.getType() != null) params.put("type", query.getType());
            if (query.getBlockHash() != null) params.put("blockHash", query.getBlockHash());
            if (query.getHeight() != null) params.put("height", query.getHeight());
            if (query.getLimit() != null) params.put("limit", query.getLimit());
            if (query.getPage() != null) params.put("page", query.getPage());

            JSONObject largeTransactionList = okxTransactionService.getLargeTransactionList(params);
            return R.ok(largeTransactionList);
        } catch (Exception e) {
            return R.fail("查询公链大额交易列表失败: " + e.getMessage());
        }
    }

    /**
     * 查询公链未确认交易列表
     */
    @Operation(summary = "查询公链未确认交易列表", description = "根据链短名称查询公链未确认交易列表")
    @GetMapping("/unconfirmed-list")
    public R<JSONObject> getUnconfirmedTransactionList(UnconfirmedTransactionListQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            if (query.getLimit() != null) params.put("limit", query.getLimit());
            if (query.getPage() != null) params.put("page", query.getPage());

            JSONObject unconfirmedTransactionList = okxTransactionService.getUnconfirmedTransactionList(params);
            return R.ok(unconfirmedTransactionList);
        } catch (Exception e) {
            return R.fail("查询公链未确认交易列表失败: " + e.getMessage());
        }
    }

    /**
     * 查询指定交易哈希内部交易
     */
    @Operation(summary = "查询指定交易哈希内部交易", description = "根据链短名称和交易哈希查询内部交易")
    @GetMapping("/internal-detail")
    public R<JSONObject> getInternalTransactionDetail(com.xdog.okx.domain.InternalTransactionQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("txId", query.getTxId());
            if (query.getLimit() != null) params.put("limit", query.getLimit());
            if (query.getPage() != null) params.put("page", query.getPage());

            JSONObject internalTransactionDetail = okxTransactionService.getInternalTransactionDetail(params);
            return R.ok(internalTransactionDetail);
        } catch (Exception e) {
            return R.fail("查询指定交易哈希内部交易失败: " + e.getMessage());
        }
    }

    /**
     * 查询指定交易哈希代币交易
     */
    @Operation(summary = "查询指定交易哈希代币交易", description = "根据链短名称和交易哈希查询代币交易")
    @GetMapping("/token-detail")
    public R<JSONObject> getTokenTransactionDetail(TokenTransactionQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("txId", query.getTxId());
            if (query.getProtocolType() != null) params.put("protocolType", query.getProtocolType());
            if (query.getLimit() != null) params.put("limit", query.getLimit());
            if (query.getPage() != null) params.put("page", query.getPage());

            JSONObject tokenTransactionDetail = okxTransactionService.getTokenTransactionDetail(params);
            return R.ok(tokenTransactionDetail);
        } catch (Exception e) {
            return R.fail("查询指定交易哈希代币交易失败: " + e.getMessage());
        }
    }

    /**
     * 查询指定交易哈希交易明细
     */
    @Operation(summary = "查询指定交易哈希交易明细", description = "根据链短名称和交易哈希查询交易明细")
    @GetMapping("/fills")
    public R<JSONObject> getTransactionFills(TransactionFillsQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("txid", query.getTxid());

            JSONObject transactionFills = okxTransactionService.getTransactionFills(params);
            return R.ok(transactionFills);
        } catch (Exception e) {
            return R.fail("查询指定交易哈希交易明细失败: " + e.getMessage());
        }
    }

    /**
     * 批量查询交易明细
     */
    @Operation(summary = "批量查询交易明细", description = "根据链短名称和交易哈希列表批量查询交易明细")
    @GetMapping("/multi")
    public R<JSONObject> getTransactionMulti(TransactionMultiQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("txId", query.getTxId());

            JSONObject transactionMulti = okxTransactionService.getTransactionMulti(params);
            return R.ok(transactionMulti);
        } catch (Exception e) {
            return R.fail("批量查询交易明细失败: " + e.getMessage());
        }
    }

    /**
     * 批量查询内部交易
     */
    @Operation(summary = "批量查询内部交易", description = "根据链短名称和交易哈希列表批量查询内部交易")
    @GetMapping("/internal-multi")
    public R<JSONObject> getInternalTransactionMulti(InternalTransactionMultiQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("txId", query.getTxId());
            if (query.getLimit() != null) params.put("limit", query.getLimit());
            if (query.getPage() != null) params.put("page", query.getPage());

            JSONObject internalTransactionMulti = okxTransactionService.getInternalTransactionMulti(params);
            return R.ok(internalTransactionMulti);
        } catch (Exception e) {
            return R.fail("批量查询内部交易失败: " + e.getMessage());
        }
    }

    /**
     * 批量查询代币交易
     */
    @Operation(summary = "批量查询代币交易", description = "根据链短名称和交易哈希列表批量查询代币交易")
    @GetMapping("/token-multi")
    public R<JSONObject> getTokenTransferMulti(TokenTransferMultiQuery query) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("chainShortName", query.getChainShortName());
            params.put("txId", query.getTxId());
            if (query.getProtocolType() != null) params.put("protocolType", query.getProtocolType());
            if (query.getLimit() != null) params.put("limit", query.getLimit());
            if (query.getPage() != null) params.put("page", query.getPage());

            JSONObject tokenTransferMulti = okxTransactionService.getTokenTransferMulti(params);
            return R.ok(tokenTransferMulti);
        } catch (Exception e) {
            return R.fail("批量查询代币交易失败: " + e.getMessage());
        }
    }
}